/*
 * @Author: 啊彪
 * @Date: 2021-08-25 20:01:11
 * @LastEditors: 啊彪
 * @LastEditTime: 2021-10-16 10:30:33
 * @Description: 统计
 * @FilePath: \fkxzg\src\controller\statistics.controller.ts
 */

import abiao_utils from "abiao_utils";
import bigNumber from "../utils/bigNumber";
import schedule, { cancelJob } from 'node-schedule';
import Server from "../models/server/server.model";
import User from "../models/user/user.model";
import SignleDayCumu from "../models/statistics/singleDayCumu.model";
import TotalCumu from "../models/statistics/totalCumu.model";

export default class statistics {

    /**
     * 总增加金币
     * @param roleId 角色ID
     * @param nickName 角色昵称
     * @param serverId 区服ID
     * @param number 数量
     */
    static async total_add_goldCoin(roleId: number, nickName: string, serverId: number, number: number) {
        let TotalCumuRes: any = await TotalCumu.findOne({ roleId });
        if (TotalCumuRes) {
            let goldCoin: string = bigNumber.jia(TotalCumuRes.goldCoin, number);
            await TotalCumu.updateOne({ roleId }, { goldCoin });
        } else {
            let info = {
                roleId,
                nickName,
                serverId,
                goldCoin: number,
            }
            await TotalCumu.create(info);
        }

        let date = abiao_utils.date(2);
        let SignleDayCumuRes: any = await SignleDayCumu.findOne({ roleId, date });
        if (SignleDayCumuRes) {
            let goldCoin: string = bigNumber.jia(TotalCumuRes.goldCoin, number);
            await SignleDayCumu.updateOne({ roleId }, { goldCoin });
        } else {
            let info = {
                roleId,
                nickName,
                serverId,
                goldCoin: number,
                date
            }
            await SignleDayCumu.create(info);
        }
    }
    /**
     * 总减少金币
     * @param roleId 角色ID
     * @param nickName 角色昵称
     * @param serverId 区服ID
     * @param number 数量
     */
    static async total_reduce_goldCoin(roleId: number, nickName: string, serverId: number, number: number) {
        let TotalCumuRes: any = await TotalCumu.findOne({ roleId });
        if (TotalCumuRes) {
            let dedGoldCoin: string = bigNumber.jian(TotalCumuRes.dedGoldCoin, number);
            await TotalCumu.updateOne({ roleId }, { dedGoldCoin });
        } else {
            let info = {
                roleId,
                nickName,
                serverId,
                dedGoldCoin: number,
            }
            await TotalCumu.create(info);
        }

        let date = abiao_utils.date(2);
        let SignleDayCumuRes: any = await SignleDayCumu.findOne({ roleId, date });
        if (SignleDayCumuRes) {
            let dedGoldCoin: string = bigNumber.jian(TotalCumuRes.dedGoldCoin, number);
            await SignleDayCumu.updateOne({ roleId }, { dedGoldCoin });
        } else {
            let info = {
                roleId,
                nickName,
                serverId,
                dedGoldCoin: number,
                date
            }
            await SignleDayCumu.create(info);
        }
    }
    /**
     * 总增加钻石
     * @param roleId 角色ID
     * @param nickName 角色昵称
     * @param serverId 区服ID
     * @param number 数量
     */
    static async total_add_diamond(roleId: number, nickName: string, serverId: number, number: number) {
        let TotalCumuRes: any = await TotalCumu.findOne({ roleId });
        if (TotalCumuRes) {
            let diamond: string = bigNumber.jia(TotalCumuRes.diamond, number);
            await TotalCumu.updateOne({ roleId }, { diamond });
        } else {
            let info = {
                roleId,
                nickName,
                serverId,
                diamond: number,
            }
            await TotalCumu.create(info);
        }

        let date = abiao_utils.date(2);
        let SignleDayCumuRes: any = await SignleDayCumu.findOne({ roleId, date });
        if (SignleDayCumuRes) {
            let diamond: string = bigNumber.jia(TotalCumuRes.diamond, number);
            await SignleDayCumu.updateOne({ roleId }, { diamond });
        } else {
            let info = {
                roleId,
                nickName,
                serverId,
                diamond: number,
                date
            }
            await SignleDayCumu.create(info);
        }

    }
    /**
     * 总减少钻石
     * @param roleId 角色ID
     * @param nickName 角色昵称
     * @param serverId 区服ID
     * @param number 数量
     */
    static async total_reduce_diamond(roleId: number, nickName: string, serverId: number, number: number) {
        let TotalCumuRes: any = await TotalCumu.findOne({ roleId });
        if (TotalCumuRes) {
            let dedDiamond: string = bigNumber.jian(TotalCumuRes.dedDiamond, number);
            await TotalCumu.updateOne({ roleId }, { dedDiamond });
        } else {
            let info = {
                roleId,
                nickName,
                serverId,
                dedDiamond: number,
            }
            await TotalCumu.create(info);
        }

        let date = abiao_utils.date(2);
        let SignleDayCumuRes: any = await SignleDayCumu.findOne({ roleId, date });
        if (SignleDayCumuRes) {
            let dedDiamond: string = bigNumber.jian(TotalCumuRes.dedDiamond, number);
            await SignleDayCumu.updateOne({ roleId }, { dedDiamond });
        } else {
            let info = {
                roleId,
                nickName,
                serverId,
                dedDiamond: number,
                date
            }
            await SignleDayCumu.create(info);
        }

    }

    /**
     * 服务器统计
     */
    static async serverStatistics() {
        var rule = new schedule.RecurrenceRule();
        rule.minute = [0, 10, 20, 30, 40, 50];
        schedule.scheduleJob(rule, async () => {
            let ServerRes: any = await Server.find({});
            for await (const iterator of ServerRes) {
                let UserCount: any = await User.countDocuments({ serverId: iterator.serverId });
                // let OrderRes: any = await Order.find({ serverId: iterator.serverId, money: { $gte: 1 }, status: 1, state: 1 });
                let money = 0;
                /* for (const item of OrderRes) {
                    money += item.money;
                } */
                await Server.updateOne({ serverId: iterator.serverId }, { userTotal: UserCount, payTotal: money });
            }
        })
    }
}
